package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/threading"
	"mzpd_server/internal/config"
	"mzpd_server/internal/handler"
	"mzpd_server/internal/svc"
	"mzpd_server/internal/utils"
	"mzpd_server/logx_ext"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/mzpdserver.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	fileWriter := logx.Reset()
	writer, err := logx_ext.NewMultiWriter(fileWriter)
	logx.Must(err)
	logx.SetWriter(writer)

	logx.DisableStat()
	//logx.Disable()

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)
	utils.GetServiceUtils().Start(c, ctx)

	threading.GoSafe(func() {
		boo, err1 := utils.GetMqttServer().Start(ctx)
		if boo || err1 != nil {
			return
		}
	})
	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
